﻿Imports CapaLogica
Imports CapaLogica.CL.PeriodoLectivo

Public Class frmBuscarCurso

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub btnBuscarCurso_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarCurso.Click
        Dim lista As New List(Of Array)
        If txtCodigo.Text = Nothing And txtNombre.Text = Nothing Then
            dtgBuscarCursos.DataSource = GestorCurso.buscarTodosCursos()
        Else
            If txtCodigo.Text <> Nothing Then
                dtgBuscarCursos.DataSource = GestorCurso.buscarCurso(txtCodigo.Text, 1)

            End If
            If txtNombre.Text <> Nothing Then
                dtgBuscarCursos.DataSource = GestorCurso.buscarCurso(txtNombre.Text, 2)
            End If
        End If
    End Sub

    Private Sub btnModificarCurso_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificarCurso.Click

        If ((dtgBuscarCursos.CurrentRow Is Nothing) OrElse (dtgBuscarCursos.SelectedRows.Count = 0)) Then
            MsgBox("Debe de seleccionar un curso antes de modificar.", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        Else
            Dim id As Int32 = CStr(dtgBuscarCursos.SelectedRows(0).Cells(0).Value)
            Dim codigo As String = CStr(dtgBuscarCursos.SelectedRows(0).Cells(1).Value)
            Dim nombre As String = CStr(dtgBuscarCursos.SelectedRows(0).Cells(2).Value)
            Dim descripcion As String = CStr(dtgBuscarCursos.SelectedRows(0).Cells(3).Value)

            Dim frmModificar As New frmModificarCurso(id, codigo, nombre, descripcion)
            frmModificar.ShowDialog()
            limpiar()

        End If
    End Sub

    Private Sub txtCodigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCodigo.TextChanged
        If txtCodigo.Text.Length > 0 Then
            txtNombre.Text = ""
            txtNombre.Enabled = False
        Else
            txtNombre.Enabled = True
        End If
    End Sub

    Private Sub txtNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNombre.TextChanged
        If txtNombre.Text.Length > 0 Then
            txtCodigo.Text = ""
            txtCodigo.Enabled = False
        Else
            txtCodigo.Enabled = True
        End If
    End Sub

    Private Sub frmBuscarCurso_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtCodigo.Enabled = True
        txtNombre.Enabled = True
    End Sub

    Private Sub btnEliminarCurso_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarCurso.Click
        Try
            GestorPeriodoLectivo.validarQuePeriodoLectivoEstaCerrado("eliminar curso")
            If ((dtgBuscarCursos.CurrentRow Is Nothing) OrElse (dtgBuscarCursos.SelectedRows.Count = 0)) Then
                MsgBox("Debe de seleccionar un curso antes de eliminarlo.", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            Else
                Dim Pregunta As Integer
                Pregunta = MsgBox("¿Esta seguro que quiere eliminar el curso del sistema?", vbYesNo + vbExclamation + vbDefaultButton2, "Eliminar Curso.")
                If Pregunta = vbYes Then
                    Dim codigo As String = CStr(dtgBuscarCursos.SelectedRows(0).Cells(1).Value)

                    GestorCurso.eliminarCurso(codigo)
                    MsgBox("El curso ha sido eliminado.")
                Else
                    MsgBox("El curso no ha sido eliminado.")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
        End Try


    End Sub

    Private Sub dtgBuscarCursos_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgBuscarCursos.CellContentClick

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        limpiar()
    End Sub

    Public Sub limpiar()
        txtCodigo.Text = ""
        txtNombre.Text = ""

        dtgBuscarCursos.DataSource = Nothing
    End Sub

    Private Sub ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Me.Dispose()
    End Sub
End Class